package com.xique.door.controller;

import com.xique.common.core.domain.R;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.core.web.domain.PageResult;
import com.xique.common.log.annotation.Log;
import com.xique.common.log.enums.BusinessType;
import com.xique.common.security.annotation.PreAuthorize;
import com.xique.door.bean.domain.OssfileUrl;
import com.xique.door.service.IOssfileUrlService;
import com.xique.door.util.FileUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/ossfileUrl")
public class OssfileUrlController {

    @Autowired
    private IOssfileUrlService ossfileUrlService;

    @PreAuthorize(hasPermi = "door:ossfileUrl:list")
    @GetMapping("/list")
    public PageResult getOssfileUrlList(
            Integer fileType,
            String title,
            Integer pageNum,
            Integer pageSize,
            String startTime,
            String endTime, String userName) {

        if (fileType == null) {
            fileType = 2;
        }
        return ossfileUrlService.getOssfileUrlList(
                fileType, title, pageNum, pageSize, startTime, endTime, null, null, userName);
    }

    @PreAuthorize(hasPermi = "door:ossfileUrl:edit")
    @GetMapping("/modify")
    public R ModifyOssfileUr(OssfileUrl ossfileUrl) {
        return ossfileUrlService.ModifyOssfileUr(ossfileUrl);
    }

    @PreAuthorize(hasPermi = "door:ossfileUrl:remove")
    @GetMapping("/remove")
    public R removeOssfileUr(OssfileUrl ossfileUrl) {
        return ossfileUrlService.ModifyOssfileUr(ossfileUrl);
    }

    @PreAuthorize(hasPermi = "door:ossfileUrl:edit")
    @GetMapping("/ossfileUrById")
    public R getOssfileUrById(OssfileUrl ossfileUrl) {
        return ossfileUrlService.getOssfileUrById(ossfileUrl);
    }

    @PreAuthorize(hasPermi = "door:ossfileUrl:add")
    @ResponseBody
    @PostMapping("/uploadVideo")
    @Log(title = "oss素材上传", businessType = BusinessType.IMPORT)
    @ApiOperation("oss素材上传")
    public AjaxResult uploadMineVideo(
            @RequestParam("file") MultipartFile file,
            Integer fileType,
            String title,
            String model,
            String type,
            String remark)
            throws Exception {
        if (file != null && fileType != null && title != null) {
            OssfileUrl oss = new OssfileUrl();
            oss.setFileType(fileType);
            oss.setTitle(title);
            oss.setModel(model);
            oss.setType(type);
            oss.setCreateBy(SecurityUtils.getUserId());
            oss.setUpdateBy(SecurityUtils.getUserId());
            oss.setRemark(remark);
            oss.setFilesize(FileUtil.FormetFileSize(file.getSize()));
            // 休眠3秒避免请求结束，文件取不到
            // 上传的文件
            ossfileUrlService.uploadMineVideo(file, oss);
            if (fileType == 1) {
                return AjaxResult.success("文件上传成功");
            } else {
                Thread.sleep(3000);
                System.out.println("文件上传中，请稍后");
                return AjaxResult.success("文件上传中，请稍后");
            }
        } else {
            return AjaxResult.error("error", "文件上传失败！！！");
        }
    }
}
